export default {
  methods: {
    /**
     * 取消订单
     *
     * @param {string} orderId - 订单ID
     * @param {number} index - 订单在列表中的索引
     */
    cancelOrder(orderId, index, type) {
      this.$api.sendRequest({
        url: "/api/order/close", 
        data: {
          order_id: orderId, 
        },
        success: res => {
          if (res.code == 0) {
            this.$util.showToast({ title: '订单取消成功' });
            if(type == 'list') {
              this.page = 1;
              this.fetchOrders(); // 刷新订单列表数据
            }else {
              this.getOrderDetail();
            }
          } else {
            this.$util.showToast({ title: res.message });
          }
        }
      });
    },
    /**
     * 确认收货
     *
     * @param {number} orderId - 订单ID
     */
    confirmReceipt(orderId,type) {
      this.$api.sendRequest({
        url: "/api/order/takedelivery", 
        data: {
          order_id: orderId, 
        },
        success: res => {
          if (res.code == 0) {
            this.$util.showToast({ title: '确认收货成功' });
            if(type == 'list') {
              this.page = 1; // 重置页码为第一页
              this.fetchOrders(); // 刷新订单列表数据
            }else {
              this.getOrderDetail();
            }
          } else {
            this.$util.showToast({ title: res.message });
          }
        }
      });
    },
    /**
     * 支付订单
     */
    payOrder(out_trade_no,type) {
      this.$api.sendRequest({
        url: "/api/pay/pay",
        data: {
          out_trade_no: out_trade_no,
          pay_type: "wechatpay"
        },
        success: res => {
          if (res.code == 0) {
              var payData = res.data.data;
              this.pay(payData,type)
          } else {
              this.$util.showToast({ title: res.message });
          }
        }
      })
    },
    goMiniProgram(data,type) {
      var that = this;
      uni.navigateToMiniProgram({
        appId: data.appid,
        path: data.path,
        extraData: {
        },
        success(res) {
          if(type == 'list') {
            that.page = 1; // 重置页码为第一页
            that.fetchOrders(); // 刷新订单列表数据
          }else {
            that.getOrderDetail();
          }
        }
      })
    },
    /**
     * 发起支付请求
     *
     * @param {Object} payData 支付数据对象
     * @param {number} payData.timeStamp 时间戳
     * @param {string} payData.nonceStr 随机字符串
     * @param {string} payData.package 订单详情扩展字符串
     * @param {string} payData.signType 签名方式
     * @param {string} payData.paySign 签名
     */
    pay(payData,type) {
      uni.requestPayment({
        provider: 'wxpay',
        timeStamp: payData.timeStamp,
        nonceStr: payData.nonceStr,
        package: payData.package,
        signType: payData.signType,
        paySign: payData.paySign,
        success: res => {
          this.$util.showToast({ title: "支付成功" });
          if(type == 'list') {
            this.page = 1; // 重置页码为第一页
            this.fetchOrders(); // 刷新订单列表数据
          }else {
            this.getOrderDetail();
          }
        },
        fail: res => {
          if (res.errMsg == 'requestPayment:fail cancel') {
            this.$util.showToast({ title: '您已取消支付' });
          } else {
            uni.showModal({ content: '支付失败,失败原因: ' + res.errMsg, showCancel: false });
          }
        }
      });
    },
    payFoodOrder(order_no,type) {
      this.$api.sendRequest({
        url: "/api/v1/shop_order/orderPay",
        data: {
          out_trade_no: order_no,
          payment_method: this.payment_method
        },
        success: res => {
          if (res.code == 1) {
            if(this.payment_method == 'balance') {
              this.$util.showToast({ title: '支付成功' });
              if(type == 'list') {
                this.page = 1; // 重置页码为第一页
                this.fetchOrders(); // 刷新订单列表数据
              }else {
                this.getOrderDetail();
              }
            }else {
              this.pay(res.data,type)
            }
          } else {
            this.$util.showToast({ title: res.msg });
          }
        }
      })
    },
    // 加餐方法
    addFoods(shop_id,out_trade_no) {
      this.$util.redirectTo('/storepages/storeDetail/storeDetail',{id: shop_id,out_trade_no:out_trade_no},'redirectTo');
    },
  }
}